package com.fiveflavors.suittar.gui.statusbar;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.fiveflavors.suittar.Suittar;

public class SuittarStatusBar extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5848604114019276679L;
	private JPanel panelLeft = null;
	private JPanel panelRight = null;

	private JLabel lblLeftSpacer = null;
	private JLabel lblRightSpacer = null;

	private JLabel lblMessage = null;
	private JProgressBar jProgressbar = null;
	private JComboBox jCombo = null;

	private String[] comboValues = new String[] {
		"none.",
		"1 min.",	
		"2 min.",	
		"3 min.",	
		"4 min.",	
		"5 min."	
	};

	public SuittarStatusBar() {
		
		panelRight = new JPanel();
		jCombo = new JComboBox(comboValues);
		jProgressbar = new JProgressBar(JProgressBar.HORIZONTAL,0,60);
		jProgressbar.setStringPainted(true);
		jProgressbar.setString(String.valueOf(jProgressbar.getMinimum()) + " sec.");
		jProgressbar.setPreferredSize(new Dimension(100,jProgressbar.getPreferredSize().height));
		lblRightSpacer = new JLabel(" ");
		
		panelRight.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panelRight.add(jCombo);
		panelRight.add(jProgressbar);
		panelRight.add(lblRightSpacer);

		panelLeft = new JPanel();
		lblMessage = new JLabel("");
		lblMessage.setPreferredSize(new Dimension(250,jCombo.getPreferredSize().height));
		lblLeftSpacer = new JLabel(" ");

		panelLeft.setLayout(new FlowLayout(FlowLayout.LEFT));
		panelLeft.add(lblLeftSpacer);
		panelLeft.add(lblMessage);

	    GridBagLayout layout = new GridBagLayout();
	    setLayout(layout);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 100.0;
        gbc.weighty = 100.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridy = 0;

        gbc.gridx = 0;
        gbc.anchor = GridBagConstraints.SOUTHWEST;
        layout.setConstraints(panelLeft, gbc);
        add(panelLeft);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.SOUTHEAST;
        layout.setConstraints(panelRight, gbc);
        add(panelRight);

		setPreferredSize(new Dimension(700, 30));		
	}
	
	public void setProgressBarValue(int value) {
		jProgressbar.setValue(value);
		jProgressbar.setString(String.valueOf(jProgressbar.getMaximum() - value) + " sec.");
	}

	public void setProgressBarMaximum(int maxValue) {
		jProgressbar.setMaximum(maxValue);
	}
	
	public void setSelectedInterval(int index) {
		if (index >= jCombo.getMaximumRowCount()) {
			return;
		}
		jCombo.setSelectedIndex(index);
		setProgressBarMaximum(60 * index);
	}
	
	public int getSelectedInterval() {
		return jCombo.getSelectedIndex();
	}
	
	public void setRefreshing(boolean indeterminate) {
		jProgressbar.setIndeterminate(indeterminate);
	}
	
	public void addSuitarStatusBarActionListener(Suittar suittar) {
		jCombo.addActionListener(new SuittarStatusBarActionListener(suittar));
	}

	public void setMessage(String message) {
		lblMessage.setText(message);
	}
	
}
